import Vue from 'vue'
import VueRouter from 'vue-router'
import register from '@/views/register.vue'
import Login from '../views/Login.vue'
import userinfo from '../views/userinfo'
import Edit from '../views/Edit'
import Home from '../views/Home.vue'
import Article from '../views/Article.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      keepAlive: true,
    }
  },
  {
    path: '/Login',
    name: 'Login',
    component: Login
  },
  {
    path: '/userinfo',
    name: 'userinfo',
    component: userinfo,
    meta:{
      istoken:true,
    }
  },
  {
    path: '/register',
    name: 'register',
    component:register,
  },
  {
    path: '/Edit',
    name: 'Edit',
    component: Edit,
    meta:{
      istoken:true,
    }
  },
  {
    path: '/Article/:id',
    name: 'Article',
    component: Article
  },
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to,from,next) =>{
  if(!localStorage.getItem('token') && !localStorage.getItem('id') && to.meta.istoken == true){
    router.push('/login')
    Vue.prototype.$msg.fail('请重新登录')
    return
  }
  next()
})

export default router
